<?php

namespace App\Models\Applet\Sponsor;

class SponsorGoodsModel extends BaseSponsorModel
{

    protected $table = 'sponsor_goods';


    const STATUS_NO  = 0; // 未上架
    const STATUS_YES = 1; // 已上架

    const IS_RECOMMEND_NO  = 0; // 未推荐
    const IS_RECOMMEND_YES = 1; // 已推荐

    /**
     * 商品列表
     * @param array $params
     * @param string[] $fields
     * @return array
     */
    public function list(array $params, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $query = $this->select($fields);

        $query = $query->where('status', self::STATUS_YES)
                       ->where('deleted', self::DELETED_NO);

        // 分类
        if (!empty($params['categoryId'])) {
            $query = $query->where('category_id', $params['categoryId']);
        }

        if (!empty($params['minPrice']) && !empty($params['maxPrice'])) {
            $query = $query->whereBetween('price', [intval($params['minPrice'] * 100), intval($params['maxPrice'] * 100)]);
        }

        // 排序
        if (!empty($params['priceSort'])) {
            $query = $query->orderBy('price', $params['priceSort']);
        } else {
            $query = $query->orderBy('is_recommend', 'desc')
                           ->orderBy('listing_time', 'desc');
        }

        if (!empty($params['timeSort'])) {
            $query = $query->orderBy('created_at', $params['priceSort']);
        }

        return $query->orderBy('id', 'desc')
                     ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
                     ->limit(PAGE_SIZE)
                     ->get()
                     ->toArray();
    }

    /**
     * 获取推荐列表
     * @param string[] $fields
     * @return array
     */
    public function recommendList(array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        return $this->select($fields)
                    ->where('status', self::STATUS_YES)
                    ->where('is_recommend', self::IS_RECOMMEND_YES)
                    ->where('deleted', self::DELETED_NO)
                    ->orderBy('id', 'desc')
                    ->offset((CURRENT_PAGE - 1) * CURRENT_PAGE)
                    ->limit(PAGE_SIZE)
                    ->get()
                    ->toArray();

    }

    /**
     * 商品列表总条数
     * @return mixed
     */
    public function adminTotal(array $params)
    {
        $query = $this;
        if (!empty($params['keyword'])) {
            $whereRaw = '(name like "%' . $params['keyword'] . '%")';
            if (is_numeric($params['keyword'])) {
                $whereRaw = trim($whereRaw, ')');
                $whereRaw .= ' or cast(id as char) like "' . $params['keyword'] . '%")';
            }
            $query = $query->whereRaw($whereRaw);
        }
        if (isset($params['isRecommend']) && $params['isRecommend'] != -1) {
            $query = $query->where('is_recommend', $params['isRecommend']);
        }
        if (isset($params['status']) && $params['status'] != -1) {
            $query = $query->where('status', $params['status']);
        }

        return $query->where('deleted', self::DELETED_NO)->count();
    }

    /**
     * 商品列表
     * @param array $params
     * @param string[] $fields
     * @return array
     */
    public function adminList(array $params, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $query = $this;
        if (!empty($params['keyword'])) {
            $whereRaw = '(name like "%' . $params['keyword'] . '%")';
            if (is_numeric($params['keyword'])) {
                $whereRaw = trim($whereRaw, ')');
                $whereRaw .= ' or cast(id as char) like "' . $params['keyword'] . '%")';
            }
            $query = $query->whereRaw($whereRaw);
        }
        if (isset($params['isRecommend']) && $params['isRecommend'] != -1) {
            $query = $query->where('is_recommend', $params['isRecommend']);
        }
        if (isset($params['status']) && $params['status'] != -1) {
            $query = $query->where('status', $params['status']);
        }

        return $query->select($fields)
                     ->where('deleted', self::DELETED_NO)
                     ->orderBy('id', 'desc')
                     ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
                     ->limit(PAGE_SIZE)
                     ->get()
                     ->toArray();
    }

    /**
     * 根据订单id和商品id查询数据
     * @return void
     */
    public function getItemByOrderIdAndGoodsId(int $orderId, int $goodsId, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
                       ->where('order_id', $orderId)
                       ->where('goods_id', $goodsId)
                       ->where('deleted', self::DELETED_NO)
                       ->first($fields);
        return $result ? $result->toArray() : [];
    }

    /**
     * 根据分类id查询数据
     * @param int $categoryId
     * @param array $fields
     * @return array
     */
    public function getItemByCategoryId(int $categoryId, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
                       ->where('category_id', $categoryId)
                       ->where('deleted', self::DELETED_NO)
                       ->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * 根据id获取列表
     * @return void
     */
    public function getItemsByIdAndDeletedAll(array $ids, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        return $this->select($fields)
                    ->whereIn('id', $ids)
                    ->get()
                    ->toArray();
    }
}
